const storageReg = /(([\w-]+)="([^"]+)")/g

module.exports = class StorageStatus {
	constructor(ret) {
		this.result = ret
		this._storageStatus = undefined
	}

	get headers() {
		return this.result.res.headers
	}

	/**
	 * 是否已经请求过解冻操作
	 * @returns {boolean}
	 */
	get isRequestRestore() {
		if (this.isStorage) {
			let fileStatus = this.headers['x-oss-restore']
			if (!fileStatus) {
				return false
			}
		}
		return true
	}

	/**
	 * 是否是归档文件
	 * @returns {boolean}
	 */
	get isStorage() {
		return StorageStatus.isStorage(this.headers['x-oss-storage-class'])
	}

	static isStorage(storageClass) {
		return ['Archive', 'ColdArchive'].includes(storageClass)
	}

	/**
	 *	请求解冻操作后，文件状态信息
	 * @returns {any}
	 */
	get restoreStatus() {
		if (this._storageStatus) return this._storageStatus
		this._storageStatus = {}
		while (storageReg.test(this.headers['x-oss-restore'])) {
			this._storageStatus[RegExp.$2] = RegExp.$3
		}
		return this._storageStatus
	}

	/**
	 * 解冻中
	 * @returns {boolean}
	 */
	get restore_ing() {
		return this.restoreStatus['ongoing-request'] === 'true'
	}

	/**
	 * 解冻完成
	 * @returns {boolean}
	 */
	get restore_end() {
		return this.restoreStatus['ongoing-request'] === 'false'
	}

}
